<?php

namespace App\Http\Controllers\Staff;

use App\Http\Controllers\Controller;
use App\Models\CommodityOrder;
use App\Models\SmsLog;
use App\Models\Staff;
use Illuminate\Http\Request;
use Overtrue\EasySms\EasySms;

class RegisterController extends Controller
{
    public function regist(Request $request)
    {

    }

    public function login(Request $request)
    {
//        $account = $request->input('phone');
//        $pwd = $request->input('pwd');
//        $staff = Staff::where('account', $account)->first();
//        if(!$staff){
//            return makeResponse([], '该用户不存在', 404);
//        }
//        if (passwordVerify($pwd, $staff->password)) {
//            unset($staff->password);
//            return makeResponse($staff, '登陆成功', 200);
//        } else {
//            return makeResponse([], '密码错误', 404);
//        }
//       上面已经修改成 手机登陆
        $region = $request->input('region');
        $lng = $request->input('lng'); //经度
        $lat = $request->input('lat');//纬度
        $phone = $request->input('phone');
        $code = $request->input('code');//验证码
        $time = time() - 360;
        $sms = SmsLog::where('phone', $phone)
            ->where('code', $code)
            ->where('created_at', '>', date('Y-m-d H:i:s', $time))
            ->orderBy('created_at', 'desc')
            ->first();

//        if (!$sms) {
//            return makeResponse([], '验证码错误', 404);
//        }
        try {

            $staff = Staff::with('enterprise')->where('account', $phone)->first();

            unset($staff->password);
            if ($staff) {
                return makeResponse($staff, '登陆成功', 200);
            }

            $staffModel = new Staff();

            $staffModel->account = $phone;
            $staffModel->staff_region = $region;
            $staffModel->staff_lng = $lng;
            $staffModel->staff_lat = $lat;
            $staffModel->save();
            $newStaff = $staffModel->find($staffModel->id);
            unset($newStaff->password);
            return makeResponse($newStaff, '登陆成功', 200);
        } catch (\Exception $exception) {
            return $this->makeResponse([], $exception->getMessage() . ' [LINE]:' . $exception->getLine(), 500);
        }

    }

    public function sendSms(Request $request)
    {

        try {
            $phone = $request->input('phone');
            $type = $request->input('type');
            $smsConfig = config('sms');
            $sms = new EasySms($smsConfig);
            $code = rand(1000, 9999);
            $data = [
//                'content'  => '您的验证码为: 6379',
                'template' => 'SMS_175240424',
                'data' => [
                    'code' => $code
                ]
            ];
            $data = $sms->send($phone, $data);
            $data = ['phone' => $phone, 'code' => $code];
            SmsLog::createOne($data)->save();
            return makeResponse([], '发送成功', 200);
        } catch (\Exception $exception) {
            return makeResponse([], "[-FILE-]:" . $exception->getFile() . "[-LINE-]:" . $exception->getLine(),
                500);
        }
    }

    public function checkSms(Request $request)
    {
        $phone = $request->input('phone');
        $code = $request->input('code');

    }

    public function forgetPassword(Request $request)
    {
        $phone = $request->input('phone');
        $code = $request->input('code');
        $pwd = $request->input('pwd');
        $cpwd = $request->input('cpwd');

        if ($pwd != $cpwd) {
            return makeResponse([], '两次密码不一致', 404);
        }
        $time = time() - 360;
        $sms = SmsLog::where('phone', $phone)
            ->where('code', $code)
            ->where('created_at', '>', date('Y-m-d H:i:s', $time))
            ->orderBy('created_at', 'desc')
            ->first();
        if (!$sms) {
            return makeResponse([], '验证码错误', 404);
        }


        $staff = Staff::where('account', $phone)->update(['password' => passwordEncode($pwd)]);
        return makeResponse([], '修改成功', 200);
    }

    public function changePassword(Request $request)
    {
        $staffId = $request->input('staff_id');
        $oldPwd = $request->input('old_pwd');
        $newPwd = $request->input('new_pwd');
        $cpwd = $request->input('cpwd');
        if ($newPwd != $cpwd) {
            return makeResponse([], '两次密码不一致', 404);
        }

        $staff = staff::where('id', $staffId)->first();
        if (passwordVerify($oldPwd, $staff->password)) {
            Staff::where('id', $staffId)->update(['password' => passwordEncode($newPwd)]);
            unset($staff->password);
            return makeResponse([], '修改成功', 200);
        } else {
            return makeResponse([], '密码错误', 404);
        }
    }

    public function userInfo(Request $request)
    {
        $staffId = $request->input('staff_id');
        $staff = Staff::with('enterprise')->find($staffId);

        $orderNum = CommodityOrder::where('order_status', 4)->whereHas('staff', function ($query) use ($staffId) {
            $query->where('staff_id', $staffId);
        })->count();
        $staff->staff_count=$orderNum;
        unset($staff->password);
        return makeResponse($staff, '查询成功', 200);

    }


}